
package demo1;
import java.util.Arrays;
import java.util.*;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString(){
        return "student:"+name+" age:"+age ;
    }
//
    @Override
    public int compareTo(Student o){
        if(this.age - o.age > 0){
            return 1;
        }else if(this.age - o.age < 0){
            return -1;
        }else{
            return 0;
        }
    }
}

//todo Comparator接口当中 不止一个抽象方法 那么为什么只需要实现一个compare方法就行
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }

class NameComparator implements Comparator<Student>{
        @Override
    public int compare(Student o1,Student o2){
            return o1.name.compareTo(o2.name);
        }

}

}


public class Test {
    public static void bubbleSort(Comparable[] array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-i-1; j++) {
                if(array[j].compareTo(array[j+1]) > 0){
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }


    public static void main(String[] args) {
//        int[] array = {1,2，3,46,7};
//        Arrays.sort(array);
//        System.out.println(Arrays.toString(array));
        Student student1 = new Student("张三",28);
        Student student2 = new Student("李四",22);
        Student student3 = new Student("王五",23);
        Student[] student = {student1,student2,student3};
        //比较器
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(student,ageComparator);
        System.out.println(Arrays.toString(student ));
        //冒泡排序
        bubbleSort(student);
        System.out.println(Arrays.toString(student));
//        Arrays.sort(student);
//        System.out.println(Arrays.toString(student));
//        if(student1.compareTo(student2) > 0){
//            System.out.println("student1 > student2");
//        }else if(student1.compareTo(student2) == 0 ){
//            System.out.println("student1 == student2");
//        }else{
//            System.out.println("student1 < student2");
//        }

    }

    public static void main1(String[] args) {
        String[] strings = {"abc","hello","bcd"};
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }
}
